#!/bin/sh
#
# create a .deb package
#

COPYALL=

SERVICE=propcap
SERVICE_FOLDER=etc/init.d
PROGRAM_FOLDER=opt
SRC_PACKAGE=./$SERVICE

FOLDERS=`echo "$SERVICE_FOLDER $PROGRAM_FOLDER"`

# Show usage
usage()
{
  echo "Usage: $0 <target folder> <version>"
  exit 1
}

# build package
buildit()
{
  SPKG=`echo ${SRC_PACKAGE}`
  SPKG=`echo ${SPKG} | sed 's/\.*\///g'`
  echo "Building debian package for ${SPKG}"

  PROPGCC=/opt/parallax
  if [ -e ${PROPGCC} ]; then
    echo "PropellerGCC found."
  else
    echo "Error ${PROPGCC} folder not found."
    exit 1
  fi

  PROGRAM=./$SRC_PACKAGE/$SERVICE
  if [ -e ${PROGRAM} ]; then
    echo "Program $SERVICE found."
  else
    echo "Error ${PROGRAM} not found."
    exit 1
  fi

  VER=$2
  PREFIX=$1-$VER
  echo "Target Package Prefix $PREFIX"

  echo "make $PREFIX folders"

  for DIR in ${FOLDERS} ; do
    MYDIR=$PREFIX/$DIR
    mkdir -p $MYDIR
    echo $MYDIR
    if [ $? -ne 0 ]; then
      echo "Error making folder $MYDIR"
      exit 1
    fi
  done

  echo "Copy service $SERVICE"
  cp ${SRC_PACKAGE}/$SERVICE $PREFIX/$SERVICE_FOLDER
  if [ $? -ne 0 ]; then
    echo "Failure copying $SERVICE"
    exit 1
  fi

  echo "Ensure $SERVICE is executable"
  chmod u+x $PREFIX/$SERVICE_FOLDER
  if [ $? -ne 0 ]; then
    echo "Failure chmod u+x $PREFIX/$SERVICE_FOLDER"
    exit 1
  fi

  echo "Copy $SRC_PACKAGE package"
  cp -r ${SRC_PACKAGE} $PREFIX/$PROGRAM_FOLDER
  if [ $? -ne 0 ]; then
    echo "Failure copying $SRC_PACKAGE"
    exit 1
  fi

  if [ -z $COPYALL ]; then
    echo "Skipping $PROPGCC copy"
  else
    echo "Copy $PROPGCC"
    cp -r ${PROPGCC} $PREFIX/$PROGRAM_FOLDER
    if [ $? -ne 0 ]; then
      echo "Failure copying $PROPGCC"
      exit 1
    fi
  fi

  echo "Make .deb Package"
  #VER=`echo ${SRC_PACKAGE} | sed 's/^.*\([0-9].*\)-\([0-9].*\)/\1\.\2/g'`
  #VER=`echo $TARGET | sed 's/^.*\([0-9].*\)-\([0-9].*\)-\([0-9].*\)/\1\.\2\.\3/g'`
  #PREFIX=`echo $TARGET | sed 's/\(^.*\)-/\1/g'`

  CPU=`uname -m`
  CPUSET=""
  if [ ${CPU} = i686 ]; then
    CPU=i386
    CPUSET=$CPU
  fi
  if [ ${CPU} = x86_64 ]; then
    CPU=amd64
    CPUSET=$CPU
  fi
  if [ ${CPU} = armv6l ]; then
    CPU=armhf
    CPUSET=$CPU
  fi
  
  if [ -z $CPUSET ]; then
    echo "CPU not modified $CPU"
  fi

  echo "Package CPU $CPU"

  mkdir -p $PREFIX/DEBIAN/
  cat $SRC_PACKAGE/debian_control | sed "s/VERSION/${VER}/g" > temp1
  cat temp1 | sed "s/CPU/${CPU}/g" > $PREFIX/DEBIAN/control

  rm -f temp1
  dpkg-deb -b $PREFIX $PREFIX-$CPU.deb
  if [ $? -ne 0 ]; then
    echo "Failure making package"
    exit 1
  fi
}

cleanup()
{
    for DIR in ${FOLDERS} ; do
      rm -rf $DIR
      if [ $? -ne 0 ]; then
        echo "Error removing $DIR"
        exit 1
      fi
    done
}

if [ -z $1 ]; then
  usage
  exit 1
fi

case $1 in

  clean)
    echo "Clean files"
    cleanup
  ;;

  ?*)
    VERSION=0.0
    if [ ! -z $2 ]; then
      VERSION=$2
    fi
    TARGET=$1
    if [ -e "$TARGET-$VERSION" ]; then
      echo "Target '$TARGET-$VERSION' exists. Exiting."
    else
      buildit $TARGET $VERSION
    fi
  ;;

esac


exit 0


